﻿using System;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using QLTH_DTO;
namespace unitlControl
{
  public class toancuc
    {

        private static clsQLNguoiDungDTO _thongtinND = new clsQLNguoiDungDTO();

        public static clsQLNguoiDungDTO ThongtinND
        {
            get { return toancuc._thongtinND; }
            set { toancuc._thongtinND = value; }
        }
      //hàm trả về MD5 cuổi 1 chuôi
      public static String GetMd5(String _chuoimahoa)
      {

          MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
          byte[] Hash = md5.ComputeHash(Encoding.UTF8.GetBytes(_chuoimahoa));
          StringBuilder SB = new StringBuilder();
          for (int i = 0; i < Hash.Length; i++)
          {
              SB.Append(Hash[i].ToString("x2"));
          }


          return SB.ToString();
      }

      //hàm lấy họ tên
      public static String[] Ho_Ten(String chuoi)
      {
          chuoi = chuoi.Trim();
          String _ho = "";
          String _ten = "";
          String[] _input = chuoi.Split(' ');
          if (_input.Length == 1)
          { 
            _ten = _input[0];
          }
          else
          {
              for (int i = 0; i <= _input.Length -2 ; i++)
              {
                  _ho += _input[i] + " ";
                 
              }
              _ho.Trim();
              _ten = _input[_input.Length - 1];
          }
          
          return new[] {_ho,_ten };

      }

      // ma nam hoc
      public static string MaNH(String _namhoc)
      {
          String _manh = "";
          String[] _tmp = _namhoc.Split(new char[] { '-' });
          _manh = "NH" + _tmp[0].Substring(2, 2) + _tmp[1].Substring(2, 2);
          return _manh;
      }


      // ma kế tiếp 
      public static string Tao_ma(String _macuoi, String _chuoi, Int16 _dodaichuoi)
      {
          String _kq = "";
          String _temp = "";
          int _so2 = 0;
          if (_macuoi == "" || _macuoi == null)
          {
              for (int i = 0; i < _dodaichuoi - 1; i++)
              {
                  _temp = _temp + "0";
              }
              return _kq = _chuoi + _temp + "1";
          }
          else
          {
              int _lenght = _macuoi.Length - _chuoi.Length;
              _so2 = int.Parse(_macuoi.Substring(_chuoi.Length, _dodaichuoi)) + 1;
              String _tso2 = _so2.ToString();
              int _so3 = Math.Abs(_tso2.Length - _lenght);
              if (_tso2.Length <= _lenght)
              {
                  for (int i = 0; i <= _so3 - 1; i++)
                  {
                      _temp = _temp + "0";
                  }
                  _kq = _chuoi + _temp + _so2;
              }
              return _kq;
          }
      }
      //đinh dạng ngày tháng
      public static DateTime ddMMyyyy(String _chuoi)

      {
          System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("vi-VN");
          culture.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
          DateTime dt = DateTime.ParseExact(_chuoi, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
          
          return dt;
      }
  
    }
}
